<?php

namespace common\widgets\webuploader;
use yii\base\Behavior;
use yii\db\ActiveRecord;

class UploaderBehavior extends Behavior {
    
    //保存[insert和update]之前的所有attribute的值
    //如$oldAttributeList['old_$attrid'] = 'old value';
    public $oldAttributeList;
    
    //需要上传有哪些字段
    public $attribute;
    
    public function events() {
        return [
            ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert',
            ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate',
            ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
            ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
            ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
        ];
    }
    
    public function beforeUpdate() {
        
        $single_img_component_list = WebUploaderUtil::getComponentList($this->owner, 'single_img');   //单图组建的列表
        $multi_img_component_list = WebUploaderUtil::getComponentList($this->owner, 'multi_img');   //多图组建的列表
        $single_file_component_list = WebUploaderUtil::getComponentList($this->owner, 'single_file');   //单文件组建的列表
        $multi_file_component_list = WebUploaderUtil::getComponentList($this->owner, 'multi_file');   //多图组建的列表
        
        if(!empty($single_img_component_list)){
            foreach ($single_img_component_list as $single_img_component){
                $this->oldAttributeList["old_{$single_img_component}"] = $this->owner->oldAttributes[$single_img_component];   //旧数据保存
                $this->owner->$single_img_component = WebUploaderUtil::getRemovedProductPath($this->owner->$single_img_component);
            }
        }
        if(!empty($multi_img_component_list)){
            foreach ($multi_img_component_list as $multi_img_component){
                $this->oldAttributeList["old_{$multi_img_component}"] = $this->owner->oldAttributes[$multi_img_component];   //旧数据保存
                $this->owner->$multi_img_component = WebUploaderUtil::getRemovedMultiProductPath($this->owner->$multi_img_component);
            }
        }
        if(!empty($single_file_component_list)){
            foreach ($single_file_component_list as $single_file_component){
                $this->oldAttributeList["old_{$single_file_component}"] = $this->owner->oldAttributes[$single_file_component];   //旧数据保存
                $this->owner->$single_file_component = WebUploaderUtil::getRemovedProductPath($this->owner->$single_file_component);
            }
        }
        if(!empty($multi_file_component_list)){
            foreach ($multi_file_component_list as $multi_file_component){
                $this->oldAttributeList["old_{$multi_file_component}"] = $this->owner->oldAttributes[$multi_file_component];   //旧数据保存
                $this->owner->$multi_file_component = WebUploaderUtil::getRemovedMultiProductPath($this->owner->$multi_file_component);
            }
        }
    
    }
    
    public function afterUpdate() {
        
        $single_img_component_list = WebUploaderUtil::getComponentList($this->owner, 'single_img');   //单图组建的列表
        $multi_img_component_list = WebUploaderUtil::getComponentList($this->owner, 'multi_img');   //多图组建的列表
        $single_file_component_list = WebUploaderUtil::getComponentList($this->owner, 'single_file');   //单文件组建的列表
        $multi_file_component_list = WebUploaderUtil::getComponentList($this->owner, 'multi_file');   //多图组建的列表
        
        if(!empty($single_img_component_list)){
            foreach ($single_img_component_list as $single_img_component){
                WebUploaderUtil::updateSingleImage($this->owner, $single_img_component);    //单图更新
            }
        }
        if(!empty($multi_img_component_list)){
            foreach ($multi_img_component_list as $multi_img_component){
                WebUploaderUtil::updateMultiImage($this->owner, $multi_img_component); //多图更新
            }
        }
        if(!empty($single_file_component_list)){
            foreach ($single_file_component_list as $single_file_component){
                WebUploaderUtil::updateSingleFile($this->owner, $single_file_component); //单文件更新
            }
        }
        if(!empty($multi_file_component_list)){
            foreach ($multi_file_component_list as $multi_file_component){
                WebUploaderUtil::updateMultiFile($this->owner,$multi_file_component);   //多文件更新
            }
        }
    
    }
    
    public function beforeInsert() {
        
        $single_img_component_list = WebUploaderUtil::getComponentList($this->owner, 'single_img');   //单图组建的列表
        $multi_img_component_list = WebUploaderUtil::getComponentList($this->owner, 'multi_img');   //多图组建的列表
        $single_file_component_list = WebUploaderUtil::getComponentList($this->owner, 'single_file');   //单文件组建的列表
        $multi_file_component_list = WebUploaderUtil::getComponentList($this->owner, 'multi_file');   //多图组建的列表
        
        if(!empty($single_img_component_list)){
            foreach ($single_img_component_list as $single_img_component){
                $this->oldAttributeList["old_{$single_img_component}"] = $this->owner->$single_img_component;   //含temp的路径地址
                $this->owner->$single_img_component = WebUploaderUtil::getRemovedProductPath($this->owner->$single_img_component);    //转换为正式地址
            }
        }
        if(!empty($multi_img_component_list)){
            foreach ($multi_img_component_list as $multi_img_component){
                $this->oldAttributeList["old_{$multi_img_component}"] = $this->owner->$multi_img_component; //含temp的路径地址
                $this->owner->$multi_img_component = WebUploaderUtil::getRemovedMultiProductPath($this->owner->$multi_img_component);   //转换为正式地址
            }
        }
        if(!empty($single_file_component_list)){
            foreach ($single_file_component_list as $single_file_component){
                $this->oldAttributeList["old_{$single_file_component}"] = $this->owner->$single_file_component; //含temp的路径地址
                $this->owner->$single_file_component = WebUploaderUtil::getRemovedProductPath($this->owner->$single_file_component);    //转换为正式地址
            }
        }
        if(!empty($multi_file_component_list)){
            foreach ($multi_file_component_list as $multi_file_component){
                $this->oldAttributeList["old_{$multi_file_component}"] = $this->owner->$multi_file_component;   //含temp的路径地址
                $this->owner->$multi_file_component = WebUploaderUtil::getRemovedMultiProductPath($this->owner->$multi_file_component); //转换为正式地址
            }
        }
        
    }
    
    public function afterInsert() {
        
        $single_img_component_list = WebUploaderUtil::getComponentList($this->owner, 'single_img');   //单图组建的列表
        $multi_img_component_list = WebUploaderUtil::getComponentList($this->owner, 'multi_img');   //多图组建的列表
        $single_file_component_list = WebUploaderUtil::getComponentList($this->owner, 'single_file');   //单文件组建的列表
        $multi_file_component_list = WebUploaderUtil::getComponentList($this->owner, 'multi_file');   //多图组建的列表
    
        if(!empty($single_img_component_list)){
            foreach ($single_img_component_list as $single_img_component){
                WebUploaderUtil::uploadSingleImage($this->owner, $single_img_component);    //将单图[临时地址] 移动到 [正式地址]
            }
        }
        if(!empty($multi_img_component_list)){
            foreach ($multi_img_component_list as $multi_img_component){
                WebUploaderUtil::uploadMultiImage($this->owner, $multi_img_component);  //将多图[临时地址] 移动到 [正式地址]
            }
        }
        if(!empty($single_file_component_list)){
            foreach ($single_file_component_list as $single_file_component){
                WebUploaderUtil::uploadSingleFile($this->owner, $single_file_component);    //将单文件[临时地址] 移动到 [正式地址]
            }
        }
        if(!empty($multi_file_component_list)){
            foreach ($multi_file_component_list as $multi_file_component){
                WebUploaderUtil::uploadMultiFile($this->owner, $multi_file_component); //将多文件[临时地址] 移动到 [正式地址]
            }
        }
    
    }
    
    public function afterDelete() {
        
        $attributeList = explode(',', $this->attribute);
        foreach ($attributeList as $attribute){
            $attribute = trim($attribute);
            if(!empty($attribute)){
                
                $attrVal = $this->owner->$attribute;
                if(WebUploaderUtil::contain($attrVal, ':')){    //文件类型
                    WebUploaderUtil::deleteMultiFile($attrVal); //文件删除
                }else{  //图片类型
                    WebUploaderUtil::deleteMultiImage($attrVal);    //图片删除
                }
                
            }
        }
    
    }
    
    
    
}